/**
 *
 * @author 陆楚良
 *
 */
class SaoLeiGrid extends egret.Sprite {
    private _gameOver:Boolean = false;
    private pieces: Array<any> = new Array(400);
    public constructor() {
        super();
        for(var i:number = 0;i < 400;i++) {
            this.pieces[i] = new Piece();
            this.pieces[i].index = i;
            this.pieces[i].x = 2 + (i % 20) * 25;
            this.pieces[i].y = 2 + parseInt(String(i / 20)) * 25;
            this.pieces[i].addEventListener(SaoLeiEvent.PIECE_OPEN,this.onOpenEvent,this);
            this.pieces[i].addEventListener(SaoLeiEvent.PIECE_BIAOJI,this.onBiaojiEvent,this);
            this.pieces[i].addEventListener(SaoLeiEvent.PIECE_CANCEL_BIAOJI,this.onCancelBiaojiEvent,this);
            this.addChild(this.pieces[i]);
        }
    }
    public gameOver(){
        this._gameOver = true;
        for(var i: number = 0;i < 400;i++) {
            this.pieces[i].over();
        }
    }
    private onBiaojiEvent(evt: SaoLeiEvent): void {
        this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_BIAOJI));
    }
    private onCancelBiaojiEvent(evt: SaoLeiEvent): void {
        this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_CANCEL_BIAOJI));
    }
    private onOpenEvent(evt: SaoLeiEvent): void {
        if(!this._gameOver){
            var target: Piece = evt.currentTarget;
            this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_OPEN));
            if(target.type === GV.T_0) {
                this.openNeighbor(target);
            } else if(target.type === GV.T_DILEI){
                this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.GAME_OVER));
            }
        }
    }
    /**
     * 将周围的打开
     */ 
    private openNeighbor(target: Piece): void {
        var x: number = target.index % 20;
        var y: number = parseInt(String(target.index / 20));
        x > 0 && y > 0 && this.pieces[(y - 1) * 20 + (x - 1)].open();
        y > 0 && this.pieces[(y - 1) * 20 + x].open();
        x < 19 && y > 0 && this.pieces[(y - 1) * 20 + (x + 1)].open();
        x > 0 && this.pieces[y * 20 + (x - 1)].open();
        x < 19 && this.pieces[y * 20 + (x + 1)].open();
        x > 0 && y < 19 && this.pieces[(y + 1) * 20 + (x - 1)].open();
        y < 19 && this.pieces[(y + 1) * 20 + x].open();
        x < 19 && y < 19 && this.pieces[(y + 1) * 20 + (x + 1)].open();
    }
	/**
	 * 随机数
	 * @param Min   最小值
	 * @param Max   最大值
	 */
    private random(Min: number,Max: number): number {
        var Range: number = Max - Min;
        var Rand: number = Math.random();
        return (Min + Math.round(Rand * Range));
    }
    
    /**
     * 计算周围的雷数
     */ 
    private count(index: number): number {
        var x: number = index % 20;
        var y: number = parseInt(String(index / 20));
        var n: number = 0;
        n += (x > 0 && y > 0 && this.pieces[(y - 1) * 20 + (x - 1)].type === GV.T_DILEI) ? 1 : 0;
        n += (y > 0 && this.pieces[(y - 1) * 20 + x].type === GV.T_DILEI) ? 1 : 0;
        n += (x < 19 && y > 0 && this.pieces[(y - 1) * 20 + (x + 1)].type === GV.T_DILEI) ? 1 : 0;
        n += (x > 0 && this.pieces[y * 20 + (x - 1)].type === GV.T_DILEI) ? 1 : 0;
        n += (x < 19 && this.pieces[y * 20 + (x + 1)].type === GV.T_DILEI) ? 1 : 0;
        n += (x > 0 && y < 19 && this.pieces[(y + 1) * 20 + (x - 1)].type === GV.T_DILEI) ? 1 : 0;
        n += (y < 19 && this.pieces[(y + 1) * 20 + x].type === GV.T_DILEI) ? 1 : 0;
        n += (x < 19 && y < 19 && this.pieces[(y + 1) * 20 + (x + 1)].type === GV.T_DILEI) ? 1 : 0;
        return n;
    }
    /**
     * 开始游戏
     */ 
    public start(): void {
        this._gameOver = false;
        var i: number;
        for(var i = 0;i < 400;i++) {
            this.pieces[i].init();
        }
        // 生成地雷
        i = 0;
        while(i < GV.count) {
            var r: number = this.random(1,400) - 1;
            if(this.pieces[r].type !== GV.T_DILEI) {
                this.pieces[r].type = GV.T_DILEI;
                i += 1;
            }
        }
        for(i = 0;i < 400;i++) {
            if(this.pieces[i].type !== GV.T_DILEI) {
                this.pieces[i].type = this.count(i);
            }
        }
    }
}
